//
//  YHGROtherNodoVC.m
//  test
//
//  Created by 李彦宏 on 2018/3/10.
//  Copyright © 2018年 朱圳. All rights reserved.
//待操作

#import "YHGROtherNodoVC.h"
#import "UnConfHead.h"//section==0头部
#import "GRNoDohead.h"//other section头部
#import "FeeDetailCell.h"//内容详情cell
#import "FeeMangerModel.h"//费用model

#import "Utility.h"

#define FeeDetailCellID2 @"FeeDetailCell"
@interface YHGROtherNodoVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *contentArry;
@property (nonatomic, strong) NSMutableArray *selectArry;//开合Arry
@property (nonatomic, strong) FeeMangerModel *model;
@end

@implementation YHGROtherNodoVC
//UItableView初始化
- (void)setupTable
{
    UITableView *tableView=[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.backgroundColor=[UIColor whiteColor];
    tableView.showsHorizontalScrollIndicator=NO;
    tableView.showsVerticalScrollIndicator=NO;
    tableView.dataSource=self;
    tableView.delegate=self;
    self.tableView.estimatedRowHeight = 120;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableView];
    self.tableView=tableView;
    [tableView registerNib:[UINib nibWithNibName:@"FeeDetailCell" bundle:nil] forCellReuseIdentifier:FeeDetailCellID2];
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [EasyShowOptions sharedEasyShowOptions].lodingShowType =  LodingShowTypeTurnAround ;
    [EasyShowOptions sharedEasyShowOptions].textStatusType =  ShowTextStatusTypeStatusBar ;
    [EasyShowLodingView showLodingText:@""];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
         [self setFeedata];
        [EasyShowLodingView hidenLoding];
    });
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTable];
    self.view.backgroundColor=[UIColor whiteColor];
   
    
  

    // Do any additional setup after loading the view.
}
#pragma -mark -----------------------------tableview datasource and delegate---------------------------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.contentArry.count+1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    //判断section的标记是否为1,如果是说明为展开,就返回真实个数,如果不是就说明是缩回,返回0.
    if ([_selectArry[section] isEqualToString:@"1"]) {
        return 1;
    }
    return 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FeeDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:FeeDetailCellID2];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    self.model=_contentArry[indexPath.section-1];
    cell.DetailTextView.text=self.model.discrabe;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}

#pragma -mark 头部的内容
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if(section==0){
        UnConfHead *head1=[[UnConfHead alloc]init];
        head1.caozuoLb.text=@"操作";
        return head1;
    }else{
        GRNoDohead *head=[[GRNoDohead alloc]init];
        
#pragma -mark ——————操作 同意/拒绝——————————————————
        [head setIsNOBlock:^(NSString *str) {
            self.model=self.contentArry[section-1]; 
            if([str isEqualToString:@"1"]){
                NSDictionary *dic=@{@"gid":Gid,
                                    @"gpass":Gpassword,
                                    @"state":str,
                                    @"cid":self.model.ID,
                                    };
                NSLog(@"-->>dic--%@", dic);
                
                [[AFNetworkTool shareManager]postJSONWithUrl:GR_FeeMagUP parmas:dic successData:^(id json) {
                    
                 //   NSLog(@"--json--%@", json);
                   
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [self.contentArry removeAllObjects];
                        [self setFeedata];
                        [MBProgressHUD showSuccess:@"同意成功"];
                        
                    });
                } failure:^(NSError *error) {
                    NSLog(@"--error--%@", error);
                }];
            }else{
                self.model=self.contentArry[section-1];
                    NSDictionary *dic=@{@"gid":Gid,
                                        @"gpass":Gpassword,
                                        @"state":str,
                                  @"cid":self.model.ID,
                                        };
                    NSLog(@"-->>dic--%@", dic);
                    
                    [[AFNetworkTool shareManager]postJSONWithUrl:GR_FeeMagUP parmas:dic successData:^(id json) {
                      
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.contentArry removeAllObjects];
                    [self setFeedata];
                    [MBProgressHUD showSuccess:@"拒绝成功"];
                        });
                    } failure:^(NSError *error) {
                        NSLog(@"--error--%@", error);
                    }];
            }
        }];
        
        self.model=_contentArry[section-1];
        [head.cont addTarget:self action:@selector(detailsBtn:) forControlEvents:UIControlEventTouchUpInside];
        NSString *Time=[Utility getDateStringWithTimeStr:self.model.time];
        head.data.text=Time;
        head.sum.text=self.model.money;
        head.cont.tag=1000+section;
        //点击按钮发亮
        
        head.cont.showsTouchWhenHighlighted = YES;
        
        return head;   
    }
}

-(void)detailsBtn:(UIButton *)button{
    if ([_selectArry[button.tag - 1000] isEqualToString:@"0"]) {
        
        [_selectArry replaceObjectAtIndex:button.tag - 1000 withObject:@"1"];
        [_tableView reloadSections:[NSIndexSet indexSetWithIndex:button.tag - 1000] withRowAnimation:UITableViewRowAnimationFade];
    }
    else
    {
        //如果当前点击的section是展开的,那么点击后就需要把它缩回,使_selectedArray对应的值为0,这样当前section返回cell的个数变成0,然后刷新这个section就行了
        
        [_selectArry replaceObjectAtIndex:button.tag - 1000 withObject:@"0"];
        [_tableView reloadSections:[NSIndexSet indexSetWithIndex:button.tag - 1000] withRowAnimation:UITableViewRowAnimationFade];
    }
    
}
#pragma -mark ——————————其他费用数据———————————————————
-(void)setFeedata{
    NSDictionary *dic=@{
                        @"gid":Gid,
                        @"gpass":Gpassword,
                        @"state":@"0",
                        };
    [[AFNetworkTool shareManager]postJSONWithUrl:GR_OtherFeeSe parmas:dic successData:^(id json) {
        [self.contentArry removeAllObjects];
    __weak typeof(self) weakslf = self;
       NSArray *arry=json[@"otherlist"];
        for (int index=0; index<arry.count; index++) {
             weakslf.model=[FeeMangerModel yy_modelWithJSON:arry[index]];
            [weakslf.contentArry addObject:weakslf.model];
        }
        for(int i=0;i<arry.count+1;i++){
            [weakslf.selectArry addObject:@"0"];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            if(self.contentArry.count==0){
                [self.tableView removeFromSuperview];
                KongDataView *Kview=[KongDataView NilDataview];
                 Kview.frame=self.view.frame;
                [self.view addSubview:Kview];
            }
            [weakslf.tableView reloadData];
        });
        
        
    } failure:^(NSError *error) {
        NSLog(@"--error--%@", error);
    }];
    
}
-(NSMutableArray *)contentArry{
    if(_contentArry==nil){
        _contentArry=[[NSMutableArray alloc]init];
    }
    return _contentArry;
}
-(NSMutableArray *)selectArry{
    if (_selectArry==nil) {
        _selectArry = [[NSMutableArray alloc]init];
    }
    return _selectArry;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
